/*
 * Copyright (c), 
 * All rights reserved.
 *
 * Change Logs:
 * Date         Author      Action
 * 2019/05/xx   zck         Create this file
 */


/**
 * @file 	drv_led.c
 * @brief 	led模块
 * @details 
 * 
 */

#include "stm32f4xx_rcc.h"
#include "stm32f4xx_gpio.h"
#include "drv_led.h"

#define LED_PERIPH_CLK_FUN				RCC_AHB1PeriphClockCmd
#define LED_PERIPH_PORT					RCC_AHB1Periph_GPIOF

#define LED_PORT						GPIOF
#define LED_D0_PIN						GPIO_Pin_9
#define LED_D1_PIN						GPIO_Pin_10

#define LED_OPEN						GPIO_ResetBits
#define LED_CLOSE						GPIO_SetBits

void LED_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;

	// 使能LED对应的GPIO时钟
	LED_PERIPH_CLK_FUN(LED_PERIPH_PORT, ENABLE);

	// LED引脚的初始化
	GPIO_InitStruct.GPIO_Pin = LED_D1_PIN | LED_D0_PIN;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(LED_PORT, &GPIO_InitStruct);

	LED_CLOSE(LED_PORT, LED_D1_PIN | LED_D0_PIN);
}

void LED_SelDevSelStatus(en_sel_dev_t dev, en_sel_status_t status)
{
	switch (dev)
	{
	case EN_SEL_DEV_D1:
		if (EN_SEL_STATUS_OPEN == status)
		{
			LED_OPEN(LED_PORT, LED_D1_PIN);
		}
		else
		{
			LED_CLOSE(LED_PORT, LED_D1_PIN);
		}
		break;

	case EN_SEL_DEV_D0:
		if (EN_SEL_STATUS_OPEN == status)
		{
			LED_OPEN(LED_PORT, LED_D0_PIN);
		}
		else
		{
			LED_CLOSE(LED_PORT, LED_D0_PIN);
		}
		break;
	
	default:
		break;
	}
}

void LED_SelDevToggle(en_sel_dev_t dev)
{
		if (EN_SEL_DEV_D0 == dev)
		{
			GPIO_ReadOutputDataBit(LED_PORT, LED_D0_PIN) ? LED_OPEN(LED_PORT, LED_D0_PIN) : LED_CLOSE(LED_PORT, LED_D0_PIN);
		}
		else if (EN_SEL_DEV_D1 == dev)
		{
			GPIO_ReadOutputDataBit(LED_PORT, LED_D1_PIN) ? LED_OPEN(LED_PORT, LED_D1_PIN) : LED_CLOSE(LED_PORT, LED_D1_PIN);
		}
}
